package pl.medec.formmanager.validators 
{
	import pl.medec.formmanager.FormManager;

	/**
	 * @author medec
	 */
	public class MultiEmailsValidator implements IValidator 
	{
		private var _details:Array;

		public function validate(inputString:String, formula:String = null):Boolean 
		{
			trace( this );
        	
			inputString = FormManager.removeExtraSpaces( inputString );
			trace( 'inputString: ' + (inputString) );
        	
			if(!inputString) return false;
        	
        	
			var emails:Array = inputString.split( ',' );
			trace( 'emails: ' + (emails.join( '#' )) );
        	
			var regEmail:RegExp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,8})+$/;
        	
			var tmp_l:uint = emails.length;
        	
			var bad:Array = [];
			var b:Boolean;
        	
			this._details = new Array( );
        	
			var e:String;
			for(var i:int = 0; i < tmp_l; i++) 
			{
				e = FormManager.trim( emails[i] );
				if(e.length > 0) 
				{
					b = regEmail.test( e );
					if(!b) 
					{
						bad.push( e );
						this._details.push( e );	
					}
				}
			}
			trace( 'bad: ', bad.length );
			
			return bad.length ? false : true;
		}

		public function get details():Object 
		{
			// TODO: Auto-generated method stub
			return this._details;
		}
	}
}
